/**create by liuhua at 2017年5月26日 上午9:46:57**/
package com.star.truffle.core.security;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.access.SecurityConfig;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

import com.star.truffle.core.security.dto.StarSecurityConfig;

public abstract class StarFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {

//	private PathMatcher matcher = new AntPathMatcher();

  public Map<RequestMatcher, StarSecurityConfig> resourceMap = null;

  public abstract void initResourceMap();

  public void loadResourceDefine() {
    initResourceMap();
  }

  public boolean ignoreSecurity(HttpServletRequest request) {
    Map.Entry<RequestMatcher, StarSecurityConfig> one = resourceMap.entrySet().stream().filter(entry -> entry.getKey().matches(request)).findAny().orElse(null);
    if(null != one) {
      return one.getValue().isIgnore();
    }
    return false;
  }
  
  @Override
  public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
    final HttpServletRequest request = ((FilterInvocation) object).getRequest();
    RequestMatcher requestMatcher = new AntPathRequestMatcher("/login");
    if (requestMatcher.matches(request)) {
      return null;
    }
    requestMatcher = new AntPathRequestMatcher("/getLoginInfo");
    if (requestMatcher.matches(request)) {
      return null;
    }
    requestMatcher = new AntPathRequestMatcher("/logout");
    if (requestMatcher.matches(request)) {
      return null;
    }
//    requestMatcher = new AntPathRequestMatcher("/api/login");
//    if (requestMatcher.matches(request)) {
//      return null;
//    }
//    requestMatcher = new AntPathRequestMatcher("/api/getLoginInfo");
//    if (requestMatcher.matches(request)) {
//      return null;
//    }
//    requestMatcher = new AntPathRequestMatcher("/api/logout");
//    if (requestMatcher.matches(request)) {
//      return null;
//    }
    if (null == resourceMap) {
      loadResourceDefine();
    }
    resourceMap.forEach((a, b) -> {
      if(a.toString().indexOf("getAreaByAreaId") != -1) {
        if(a.matches(request)) {
          System.out.println(1);
        }
      }
    });
    Map.Entry<RequestMatcher, StarSecurityConfig> one = resourceMap.entrySet().stream().filter(entry -> entry.getKey().matches(request)).findAny().orElse(null);
    Collection<ConfigAttribute> list = null;
    if(null != one) {
      StarSecurityConfig ssc = one.getValue();
      if(ssc.isIgnore()) {
        return null;
      }
      list = ssc.getAttributes();
    }else {
      list = Arrays.asList(new SecurityConfig("ROLE_suoyourendoumeiyouzhegequanxian"));
    }
    return list;
  }

  @Override
  public Collection<ConfigAttribute> getAllConfigAttributes() {
    if (null == resourceMap) {
      loadResourceDefine();
    }
    return resourceMap.entrySet().stream().flatMap(entry -> entry.getValue().getAttributes().stream()).collect(Collectors.toSet());
  }

  @Override
  public boolean supports(Class<?> clazz) {
    return FilterInvocation.class.isAssignableFrom(clazz);
  }

}
